#!/usr/bin/env bash

source "$rvm_scripts_path/base"
source "$rvm_scripts_path/functions/group"
source "$rvm_scripts_path/functions/utility"
source "$rvm_scripts_path/functions/version"

group_add()
{
  \typeset user group
  group="$1"
  shift
  for user in "$@"
  do
    if
      __rvm_is_user_in_group "$group" "$user"
    then
      echo "User '$user' already in the group '$group'"
    else
      echo "Adding user '$user' to the group '$group'"
      __rvm_add_user_to_group "$group" "$user"
    fi
  done
}

group_create()
{
  \typeset -a _params
  _params=( "${1:-$rvm_group_name}" )
  shift
  if
    (( $# ))
  then
    _params+=( "$1" )
  else
    if [[ "$1" == "${rvm_group_name}" && -n "${rvm_group_id:-}" ]]
    then _params+=( "${rvm_group_id:-}" )
    fi
  fi
  __rvm_create_group "${_params[@]}"
}

action="$1"
shift

true "${rvm_group_name:=rvm}"

case "$action" in
  add|create|help)
    group_$action "$@"
    ;;
  *)
    rvm_help group "$@"
    false
    ;;
esac
